package ModelLayer;
import java.util.*;

/**
 * Write a description of class LoanList here.
 * 
 * @author (Group1:Georgi,Roland and Mindaugas) 
 * @version (a version number or a date)
 */
public class LoanList
{   
    
   
    private ArrayList<Loan> loans;
    private static LoanList loanList;
    
    public LoanList()
    {
        loans = new ArrayList<Loan>();
        
    }
    
     /**
      * Returns an existing List of Loans and if there is no list of loand it creates one.
      */
    public static LoanList getLoanList()
    {
        if(loanList==null)
          loanList=new LoanList();
          
       return  loanList; 
          
    }
    
    /**
     * Adds a Loan to the List of loans
     */
    private void addLoan(Loan loan)
    {
        loans.add(loan);
    }
    
    /**
     * Searches for a Loan in the Loan List.
     */
    public Loan searchLoan(Person person,Copy copy)
    {
        for(int i = 0; i<loans.size(); i++){
           
           if(loans.get(i).getPerson()==person && loans.get(i).getCopy()==copy){
               return loans.get(i);
            }
           
        }
       return null;
    }
    
    /**
     * Checks if a loan Exists in the Loan List using its ID.
     */
    public boolean loanExist(long id)
    {
      if(searchLoan(id)==null)
         return false;
       
       else
         return true;
    }
    
    /**
     * Searches for a loan in the Loan List using its ID.
     */
    public Loan searchLoan(long id)
    {
        for(int i = 0; i<loans.size(); i++){
           
           if(loans.get(i).getID()==id){
               return loans.get(i);
            }
           
        }
      return null;
    }
    
    /**
     * Creates a new Loan and makes sure that two loans with the same ID cannot be created.
     */
     public Loan createLoan(long loanID,int borrowPeriod,Person person, Copy copy)
    {
        if(searchLoan(person,copy) == null)
        {
            Loan loan = new Loan(loanID,borrowPeriod, person, copy);
            addLoan(loan);
            return loan;
        }
        
        throw new RuntimeException();
    }
    
    /**
     * Creates a new loan.
     */
    public void createLoan(long id, String borrowDate, int period,String state, Person person, Copy copy)
    {
        if(searchLoan(id) == null)
        {
            Loan loan = new Loan(id, borrowDate, period, state, person, copy);
            addLoan(loan);
        }
         throw new RuntimeException();
    }
    
    /**
     * Searches for a loan using its id and it removes it.
     */
    public void deleteLoan(long id)
    {
        loans.remove(searchLoan(id));
    }
    
    /**
     * Deletes a Loan.
     */
        public void deleteLoan(Loan loan)
    {
         loans.remove(loan);
    }
    

}
     
    